<template>
    <n-drawer :show="active" :width="width" :mask-closable="false" @update:show="(flag:boolean) => (active = flag)">
        <n-drawer-content :native-scrollbar="false" title="退款" closable>
            <n-space vertical :size="12">
                <n-alert title="提示" type="warning">您将对以下支付单进行退款操作</n-alert>
                <n-list bordered>
                    <n-list-item>
                        <n-space>
                            <n-text code>{{ item.id }}</n-text>
                            <n-text>{{ item.memo }}</n-text>
                            <n-text>已支付 {{ Filters.money(item.value) }} 元</n-text>
                        </n-space>
                    </n-list-item>
                </n-list>

                <n-divider />
                <n-form :model="model" ref="formRef">
                    <n-form-item label="退款金额">
                        <DecimalInput :border="true" :precision="2" :width="100" suffix="元" :min="0.01" :max="item.value / 100" v-model:base="model.value" />
                    </n-form-item>
                </n-form>
            </n-space>
            <template #footer>
                <n-space justify="end">
                    <n-button type="tertiary" @click="() => (active = false)">
                        <template #icon>
                            <n-icon :component="CloseRound" />
                        </template>
                        关闭
                    </n-button>
                    <n-button type="warning" :loading="loading" @click="execSave">
                        <template #icon>
                            <n-icon :component="CheckRound" />
                        </template>
                        提交退款
                    </n-button>
                </n-space>
            </template>
        </n-drawer-content>
    </n-drawer>
</template>

<script lang="ts" setup>
import { computed, ref, onBeforeMount, watch } from 'vue'
import { CloseRound, CheckRound } from '@vicons/material'

import { Http, Filters } from '@/libs'
import { useStore } from '@/store'
import DecimalInput from '@/comp/func/DecimalInput.vue'

const store = useStore()
const width = computed(() => Math.max(store.state.browser.innerWidth * 0.4, 640))

const { $notification: notification } = window

const loading = ref(false)

const prop = defineProps({
    show: {
        type: Boolean,
        required: true,
    },
    item: {
        type: Object,
        default: {},
    },
})

const emit = defineEmits(['update:show', 'refresh'])

const active = computed({
    get: () => prop.show,
    set: val => emit('update:show', val),
})

const formRef = ref<any>(null)

const model = ref<any>({})

const execReset = () => {
    model.value = {
        id: prop.item.id,
        value: prop.item.value,
    }
}

const execSave = (e: Event) => {
    e.preventDefault()
    formRef.value.validate((errors: any[]) => {
        if (!errors) {
            loading.value = true
            Http.post('/ops/fin/online/payment/refund', model.value).then(resp => {
                if (resp.stat.err > 0) {
                    loading.value = false
                    return
                }
                emit('refresh', {})
                notification.success({
                    duration: 1500,
                    title: '操作成功',
                    content: '您已成功提交退款请求',
                })
                setTimeout(() => {
                    loading.value = false
                    emit('update:show', false)
                }, 200)
            })
        } else {
            let msgs = errors.map(err => err[0].message)
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '当前表单存在以下问题，请处理后提交',
                content: msgs.join('\n'),
            })
        }
    })
}

onBeforeMount(() => {
    execReset()
})
</script>
